﻿// -----------------------------------------------------------------------
// <copyright file="AttachmentJob_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class AttachmentJob_DAL :BaseDAL
    {
        
        // Lấy tất cả AttachmentsJob
        public static List<AttachmentsJob> GetAll(int shipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.AttachmentsJobs);
            return DB.AttachmentsJobs.Where(n => n.Job.Equipment.Ship_ID == shipID).ToList();
        }
        // Lấy AttachmentsJob theo Id
        public static AttachmentsJob GetDataById(int id)
        {
            return DB.AttachmentsJobs.Where(m => m.Attachment_ID== id).FirstOrDefault();
        }
        //Thêm một AttachmentsJob
        public static bool Insert(AttachmentsJob AttachmentsJob)
        {
            try
            {
                DB.AttachmentsJobs.InsertOnSubmit(AttachmentsJob);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        // Xóa một AttachmentsJob
        public static bool Delete(AttachmentsJob AttachmentsJob)
        {
            try
            {
                DB.AttachmentsJobs.DeleteOnSubmit(AttachmentsJob);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Cập nhật AttachmentsJob
        public static bool Update(AttachmentsJob AttachmentsJob)
        {
            try
            {
                //AttachmentsJob objAttachment = GetDataById(AttachmentsJob.Attachment_ID);
                //objAttachment.Code = AttachmentsJob.Code;
                //objAttachment.Job_ID = AttachmentsJob.Job_ID;
                //objAttachment.ContentFile = AttachmentsJob.ContentFile;
                //objAttachment.Name = AttachmentsJob.Name;
                //objAttachment.Extentsion = AttachmentsJob.Extentsion;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // Kiểm tra xem AttachmentsJob Code có tồn tại chưa ?
        public static bool IsExits(AttachmentsJob AttachmentsJob)
        {
            AttachmentsJob objAttachmentsJob = null;
            objAttachmentsJob = DB.AttachmentsJobs.Where(m => (m.Code == AttachmentsJob.Code) && 
                (m.Attachment_ID != AttachmentsJob.Attachment_ID) && (m.Job_ID == AttachmentsJob.Job_ID)).FirstOrDefault();

            return objAttachmentsJob != null ? true : false;
        }

        public static bool IsExitsID(AttachmentsJob AttachmentsJob)
        {
            AttachmentsJob objAttachmentsJob = null;
            objAttachmentsJob = DB.AttachmentsJobs.Where(m =>(m.Attachment_ID == AttachmentsJob.Attachment_ID) && (m.Job_ID == AttachmentsJob.Job_ID)).FirstOrDefault();
            return objAttachmentsJob != null ? true : false;
        }

        public static List<AttachmentsJob> GetDataByJobID(int id, bool isChanged)
        {
            if (isChanged)
            {
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.AttachmentsJobs);
            }
            return DB.AttachmentsJobs.Where(j => j.Job_ID == id).ToList();
        }
        public static AttachmentsJob GetLastData()
        {
            return DB.AttachmentsJobs.ToArray().LastOrDefault();
        }

    }
}
